﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace MSPE.Public.FOSS.AutoStatusPlus.Common
{
    internal class Settings
    {
        #region Public Properties
        internal bool DbUseIntegratedAuth { get; set; }
        internal bool LogTimesheetEvents { get; set; }
        internal bool LogUpdates { get; set; }
        internal bool NewLogFileEveryHour { get; set; }
        internal bool ProcessBlankLines { get; set; }
        internal bool SubmitUpdates { get; set; }
        internal bool WebIsConfigured { get; set; }
        internal int PollingInterval { get; set; }
        internal string DbName { get; set; }
        internal string DbPassword { get; set; }
        internal string DbServer { get; set; }
        internal string DbUser { get; set; }
        internal string LogFilePath { get; set; }
        internal string LogFilePrefix { get; set; }
        internal string SspHostname { get; set; }
        internal string SspName { get; set; }
        internal TimesheetEventEnum Event { get; set; }
        #endregion // Public Properties

        #region Constructors
        /// <summary>
        /// Constructs a new instance of the <see cref="Settings" /> class.
        /// </summary>
        internal Settings()
        {
        } // Settings()
        #endregion // Constructors

        #region Methods
        internal static Settings LoadSettings(SPWeb pwaWeb)
        {
            if (pwaWeb == null)
                throw new ArgumentNullException("pwaWeb", "Caller must provide a valid non-null SPWeb object.");

            string _event = Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.EventKey);
            
            if (!Enum.IsDefined(typeof(TimesheetEventEnum), _event))
                throw new Exception(_event + " is not a valid TimesheetEvent.");

            Settings _loadedSettings = new Settings();

            _loadedSettings.DbName = Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.DbNameKey);
            _loadedSettings.DbPassword = Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.DbPasswordKey);
            _loadedSettings.DbServer = Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.DbServerKey);
            _loadedSettings.DbUseIntegratedAuth = Convert.ToBoolean(Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.DbUseIntegratedAuthKey));
            _loadedSettings.DbUser = Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.DbUserKey);
            _loadedSettings.Event = (TimesheetEventEnum)Enum.Parse(typeof(TimesheetEventEnum), _event, true);
            _loadedSettings.LogFilePath = Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.LogFilePathKey);
            _loadedSettings.LogFilePrefix = Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.LogFilePrefixKey);
            _loadedSettings.LogTimesheetEvents = Convert.ToBoolean(Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.LogTimesheetEventsKey));
            _loadedSettings.LogUpdates = Convert.ToBoolean(Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.LogUpdatesKey));
            _loadedSettings.NewLogFileEveryHour = Convert.ToBoolean(Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.NewLogFileEveryHourKey));
            _loadedSettings.PollingInterval = Convert.ToInt32(Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.PollingIntervalKey));
            _loadedSettings.ProcessBlankLines = Convert.ToBoolean(Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.ProcessBlankLinesKey));
            _loadedSettings.SspHostname = Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.SspHostnameKey);
            _loadedSettings.SspName = Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.SspNameKey);
            _loadedSettings.SubmitUpdates = Convert.ToBoolean(Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.SubmitUpdatesKey));
            _loadedSettings.WebIsConfigured = Convert.ToBoolean(Utility.ReadConfigurationKey(pwaWeb, GlobalConstants.WebIsConfiguredKey));

            return _loadedSettings;
        } // LoadSettings()

        internal static void UpdateSettings(SPWeb pwaWeb, Settings changedSettings)
        {
            if (pwaWeb == null)
                throw new ArgumentNullException("pwaWeb", "Caller must provide a valid non-null SPWeb object.");

            SPPropertyBag _pwaWebProperties = pwaWeb.Properties;

            if (_pwaWebProperties == null)
                throw new Exception("Unable to retrieve properties for Site at URL " + pwaWeb.Url + " .");

            if (changedSettings == null)
                throw new ArgumentNullException("changedSettings", "Caller must provide a valid non-null Settings object.");

            pwaWeb.AllowUnsafeUpdates = true;

            _pwaWebProperties[GlobalConstants.DbNameKey] = changedSettings.DbName;
            _pwaWebProperties[GlobalConstants.DbPasswordKey] = changedSettings.DbPassword;
            _pwaWebProperties[GlobalConstants.DbServerKey] = changedSettings.DbServer;
            _pwaWebProperties[GlobalConstants.DbUseIntegratedAuthKey] = changedSettings.DbUseIntegratedAuth.ToString();
            _pwaWebProperties[GlobalConstants.DbUserKey] = changedSettings.DbUser;
            _pwaWebProperties[GlobalConstants.EventKey] = changedSettings.Event.ToString();
            _pwaWebProperties[GlobalConstants.LogFilePathKey] = changedSettings.LogFilePath;
            _pwaWebProperties[GlobalConstants.LogFilePrefixKey] = changedSettings.LogFilePrefix;
            _pwaWebProperties[GlobalConstants.LogTimesheetEventsKey] = changedSettings.LogTimesheetEvents.ToString();
            _pwaWebProperties[GlobalConstants.LogUpdatesKey] = changedSettings.LogUpdates.ToString();
            _pwaWebProperties[GlobalConstants.NewLogFileEveryHourKey] = changedSettings.NewLogFileEveryHour.ToString();
            _pwaWebProperties[GlobalConstants.PollingIntervalKey] = changedSettings.PollingInterval.ToString();
            _pwaWebProperties[GlobalConstants.ProcessBlankLinesKey] = changedSettings.ProcessBlankLines.ToString();
            _pwaWebProperties[GlobalConstants.SspHostnameKey] = changedSettings.SspHostname;
            _pwaWebProperties[GlobalConstants.SspNameKey] = changedSettings.SspName;
            _pwaWebProperties[GlobalConstants.SubmitUpdatesKey] = changedSettings.SubmitUpdates.ToString();
            _pwaWebProperties[GlobalConstants.WebIsConfiguredKey] = changedSettings.WebIsConfigured.ToString();
            
            _pwaWebProperties.Update();
            pwaWeb.Update();
            pwaWeb.AllowUnsafeUpdates = false;
        } // UpdateSettings()
        #endregion // Methods
    }
}
